package exist;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//import kawa.standard.thisRef;

import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;
import org.xmldb.api.modules.XPathQueryService;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;


public class ConnectToeXist extends HttpServlet {
	
	protected static String URI= "xmldb:exist://localhost:8086/exist/xmlrpc/mondial.xml";
	private static String driver = "org.exist.xmldb.DatabaseImpl";
	public  static String requette;
	private static Collection col=null;
	private static  XPathQueryService service=null; 
	private static String userName;
	private static String password;
	/*
	public ConnectToeXist(){
		init();
	}	*/
	
	/*public void init(){		 
		 ServletContext context = getServletConfig().getServletContext();
		 URI="xmldb:exist://localhost:8086/exist/xmlrpc/mondial.xml";
		 userName="admin";
		 password="admin";		
	}*/
	
	public static void connect() throws ClassNotFoundException, InstantiationException, IllegalAccessException, XMLDBException{
		   		
		   // initialize database driver	
		
	       Class cl = Class.forName(driver);
	       Database database = (Database) cl.newInstance();
	       DatabaseManager.registerDatabase(database);	       
	       // get the collection
	       col = DatabaseManager.getCollection(URI,"admin","admin" );
	      // col = DatabaseManager.getCollection(URI + "mondial.xml",userName,password );
	       service = (XPathQueryService) col.getService("XPathQueryService", "1.0");
	}
	
	public static Collection xmlCollection(){
		return col;
	}
	
	public static XPathQueryService getService(){
		return service;
	}	
	
}
